﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuChuyenKho
{
    public partial class K_PhieuChuyenKho_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "PhieuChuyenKho"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public K_PhieuChuyenKho_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public K_PhieuChuyenKho_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuChuyenKho.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.K_PhieuChuyenKho_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.K_PhieuChuyenKho_MoreInformation_GroupBox, controls);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource K_PhieuChuyenKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_PhieuChuyenKho_ViewModelSource"];
                if (Khoa == null)
                {
                    K_PhieuChuyenKho_ViewModel K_PhieuChuyenKho_ViewModel = new K_PhieuChuyenKho_ViewModel();
                    K_PhieuChuyenKho_ViewModel.ChildList_AddedNew += new K_PhieuChuyenKho_ViewModel.ChildList_AddedNew_EventHandler(K_PhieuChuyenKho_ViewModel_ChildList_AddedNew);
                    
                    K_PhieuChuyenKho_ViewModel.TCHangHoaChanged += new EventHandler(K_PhieuChuyenKho_TinhChatHangHoaChanged);
                    K_PhieuChuyenKho_ViewModel.hangHoaIdChanged += new K_PhieuChuyenKho_ViewModel.HangHoaIdChanged_EventHandler(K_PhieuChuyenKho_HangHoaIdChanged);
                   
                    K_PhieuChuyenKho_CollectionViewSource.Source = new List<object> { K_PhieuChuyenKho_ViewModel };

                    
                }
                else
                {
                    K_PhieuChuyenKho_ViewModel K_PhieuChuyenKho_ViewModel = new K_PhieuChuyenKho_ViewModel((int)Khoa);
                    K_PhieuChuyenKho_ViewModel.ChildList_AddedNew += new K_PhieuChuyenKho_ViewModel.ChildList_AddedNew_EventHandler(K_PhieuChuyenKho_ViewModel_ChildList_AddedNew);
                    K_PhieuChuyenKho_ViewModel.TCHangHoaChanged += new EventHandler(K_PhieuChuyenKho_TinhChatHangHoaChanged);
                    K_PhieuChuyenKho_ViewModel.hangHoaIdChanged += new K_PhieuChuyenKho_ViewModel.HangHoaIdChanged_EventHandler(K_PhieuChuyenKho_HangHoaIdChanged);
                   
                    K_PhieuChuyenKho_CollectionViewSource.Source = new List<object> { K_PhieuChuyenKho_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_TatCa);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_Kho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_CollectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

                DoFilterOnDefaultDM_SanPham();

                this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);


                ((ControlBehavior.GridViewKeyboardCommandProvider)this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
               
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                K_PhieuChuyenKho_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietTab.IsSelected = true;
                }
               
               
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietTab.IsSelected)
            {
                AddPhieuChuyenKhoDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietTab.IsSelected)
            {
                return K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView;
            }
          
  
            return null;
        }



        void DeleteItems()
        {
           if (this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietTab.IsSelected)
            {
                K_PhieuChuyenKho_ChiTietChild ChiTietChild = (K_PhieuChuyenKho_ChiTietChild)(this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView.SelectedItem);
                IList<K_PhieuChuyenKho_ChiTietChild> list = GetViewModel().Model.K_PhieuChuyenKho_ChiTiets;
                int index = 0;
                foreach (K_PhieuChuyenKho_ChiTietChild item in list)
                {
                    if (ChiTietChild.GetHashCode() == item.GetHashCode())
                    {
                        break;
                    }
                    index++;
                }


                DeleteMuaHangDetailsItem(index);
            }
           
            
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           K_PhieuChuyenKho_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddPhieuChuyenKhoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddPhieuChuyenKhoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddPhieuChuyenKhoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            this.tabControl.SelectedItem = tabControl.Items[0];
            this.AddPhieuChuyenKhoDetailsItem();
        }


        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            this.tabControl.SelectedItem = tabControl.Items[0];
            FocusAfterChangeTab(this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView, e.Index);
        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                grid.Focus();
                grid.BeginEdit();
            }));


        }
        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

      
        private void K_PhieuChuyenKho_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        #endregion

        #region View Model Related Methods
        private K_PhieuChuyenKho_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource K_PhieuChuyenKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_PhieuChuyenKho_ViewModelSource"];
            return ((List<object>)K_PhieuChuyenKho_CollectionViewSource.Source)[0] as K_PhieuChuyenKho_ViewModel;
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_TatCa);
        }


        public void DoFilterOnDefaultDM_SanPham()
        {
           
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };
           
        }
     

        void K_PhieuChuyenKho_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            int? tinhChatHangHoaId = this.GetViewModel().Model.TinhChatHangHoaId;
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            dm_HangHoa_ListViewModel.GetDM_HangHoa((int)tinhChatHangHoaId);
              
           
        }

        
        void K_PhieuChuyenKho_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
           
            K_PhieuChuyenKho_ChiTietChild ChiTietChild =  GetViewModel().Model.K_PhieuChuyenKho_ChiTiets[e.Index];
           
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

                
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.DonGia = (double) item.DonGiaBan;
                    ChiTietChild.DVTId = item.DonViTinhId;
                    break;
                }
            }
            
        }
     

       
        private void AddPhieuChuyenKhoDetailsItem()
        {
            GetViewModel().AddK_PhieuChuyenKho_ChiTietItem();
            
        }

        private void DeleteMuaHangDetailsItem(int Index)
        {
            GetViewModel().DeleteK_PhieuChuyenKho_ChiTietItem( Index);
        }

       
        

        void K_PhieuChuyenKho_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "K_PhieuChuyenKho_ChiTiets")
            {
                this.K_PhieuChuyenKho_PhieuChuyenKhoChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            } 
        }
        #endregion
        
    }
}
